package org.pih.migration.load.objects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom.Element;

public class Step {

	public enum ItemType {QUESTION, ACTION}
	public enum ItemFormat {CHECK_BOX, NARRATIVE}

	private String id;	
	private String itemType;
	private String itemFormat;
	private String prompt;
	private List<Option> options = new ArrayList<Option>();

	private transient Logger debugLog = Logger.getLogger("debugLog");
	private transient Logger errorLog = Logger.getLogger("errorLog");
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getItemType() {
		return itemType;
	}
	public void setItemType(String itemType) {
		this.itemType = itemType;
	}
	public String getItemFormat() {
		return itemFormat;
	}
	public void setItemFormat(String itemFormat) {
		this.itemFormat = itemFormat;
	}
	public List<Option> getOptions() {
		return options;
	}
	public void setOptions(List<Option> options) {
		this.options = options;
	}
	public void setPrompt(String prompt) {
		this.prompt = prompt;
	}
	public String getPrompt() {
		return prompt;
	}

	public String toString() {
		StringBuilder optionString = new StringBuilder();
		if (options != null) {
			for (Option option : options) {
				if (option.getValue() != null) {
					optionString.append("\n       ==> " + option.getValue() + ") " + option.getName() + " (Go to: " + option.getChildId() + ")");
				} else if (option.getChildId() != null) {
					optionString.append("\n       ==> Go to: " + option.getChildId() + ")");
				}
			}
		}

		return prompt + optionString.toString();
	}

	public void printControlTree(String parentId, Element body, Element model, Element data, HashMap<String, Integer> keyMap, Hashtable<String, Step> stepTable) {

		String id = (parentId != null && parentId.trim().length() > 0 ? parentId + "." : "") + this.id;
		
		debugLog.debug("step: " + id + ": " + this.toString());

//		int keyIncrement = 1;
//		
//		if (keyMap.containsKey(id)) {
//			keyIncrement = keyMap.get(id);
//			keyMap.put(id, ++keyIncrement);
//		} else {
//			keyMap.put(id, keyIncrement);
//		}
//		
//		if ((id + "_" + keyIncrement).equalsIgnoreCase("C1_F_4"))
//			System.out.println("break point");
		
		errorLog.debug("step: " + id + this.toString());
		Element control = new Element("select1");
		control.setAttribute("ref", id);
//		Element label = new Element("label").setText(id + ": " + prompt);
		Element label = new Element("label").setText(prompt);
		control.addContent(label);
		body.addContent(control);
		
		data.addContent(new Element(id));

		for (Option option : options) {
			if (option.getChildId() != null && option.getChildId().trim().length() > 0) {
				if (control != null) {
					Element item = new Element("item");
					Element itemLabel = new Element("label").setText(option.getName());
					Element itemValue = new Element("value").setText(option.getValue());
					item.addContent(itemLabel);
					item.addContent(itemValue);
					control.addContent(item);
				}

				if (!option.getChildId().equalsIgnoreCase("done")) {
					if (stepTable.get(option.getChildId()) != null) {
						Element bind = new Element("bind");
						bind.setAttribute("nodeset", id + "." + option.getChildId());
						bind.setAttribute("relevant", "../" + id + "='" + option.getValue() + "'");
						model.addContent(bind);
						Step step = stepTable.get(option.getChildId());
						if (this.id.equalsIgnoreCase(option.getChildId())) {
							errorLog.error("ERROR: recursive loop detected: Step: " + id + " attempts to go to " + option.getChildId());
						} else {
							if (step != null) {
								step.printControlTree(id, body, model, data, keyMap, stepTable);
							} else {
								errorLog.error("ERROR: missing step: Step is declared as an Action but not as an Item: " + option.getChildId());
							}
						}
					} else {
						errorLog.error("ERROR: missing step: Step is declared as an Action but not as an Item: " + option.getChildId());						
					}
				}
				

			}
		}
	}

}
